package cn.yuli.redis.annotation;

import java.lang.reflect.Method;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;
@Component
public class AnnotationContext implements ApplicationContextAware
, ApplicationListener<ContextRefreshedEvent> {
	ApplicationContext applicationContext;
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		this.applicationContext = applicationContext;
	}
	public void onApplicationEvent(ContextRefreshedEvent event) {
		Class<CacheResult> annotationType = CacheResult.class;
		//从容器里拿到所有加了注解的bean
		Map<String, Object> annotation = applicationContext.getBeansWithAnnotation(annotationType);
		Set<Entry<String,Object>> entrySet = annotation.entrySet();
		for (Entry<String, Object> entry : entrySet) {
			System.out.println(entry.getKey());
			CacheResult cr = AnnotationUtils.findAnnotation(entry.getValue().getClass(), annotationType);
			System.out.println(cr.key());
			System.out.println(cr.cacheName());
			Method[] methods = entry.getValue().getClass().getMethods();
			for (Method method : methods) {
				System.out.println(method.getName());
			}
		}
		
	}



}
